<?php

// 签到接口
class ControllerAccountSign extends Controller
{

    private $error = array();

    // 签到
    public function index()
    {
        if (! $this->actionCheck(true, false)) {
            return;
        }
        $this->load->model('account/sign');
        // 判断今天是否签到
        $todayBegin = date('Y-m-d') . " 00:00:00";
        $todayEnd = date('Y-m-d') . " 23:59:59";
        $sign = $this->model_account_sign->getSign();
        $today_isexit = $this->model_account_sign->getSign($todayBegin, $todayEnd);
        if ($today_isexit) {
            $this->response->setDataResult(0, '今天已经签到过了', null);
            return;
        }
        if ($sign) {
            // 存在签到
            if ((time() - strtotime($sign['last_date']) > 24 * 60 * 60)) { // 判断时间是否大于24小时
                //连续签到的天数变为1
                $this->model_account_sign->updateSign(array(
                    'num' => 1
                ));
            }elseif ((int)$sign['num']==5){
                //连续签到的天数变为1
                $this->model_account_sign->updateSign(array(
                    'num' => 1
                ));
            } else {
                $this->model_account_sign->updateSign(array(
                    'num' =>(int)$sign['num']+1
                ));
            }
        } else {
            // 从没签到过
            $this->model_account_sign->addSign(array(
                'num' => 1
            ));
        }
        $this->model_account_sign->addSignRecord(array(
            'num' => 1
        ));

        $customer_group_id = $this->customer->getGroupId();
        $this->load->model('account/reward');
        if((int)$customer_group_id==3){
           // VIP每次签到3积分
            $this->model_account_reward->addReward($this->customer->getId(),'VIP用户每日签到可获得赠送3积分',3);
        }
        //普通会员每次签到给2积分
        else{
            $this->model_account_reward->addReward($this->customer->getId(),'普通用户每日签到可获得赠送2积分',2);
        }

        $now_sign = $this->model_account_sign->getSign();

        if((int)$now_sign['num']==5){
            //VIP用户连续5天签到再多送15积分
            if((int)$customer_group_id==3){
                $this->model_account_reward->addReward($this->customer->getId(),'VIP用户连续签到5天,获得赠送积分15积分',15);
            }
            //普通用户连续5天签到再多送10积分
            else{
                $this->model_account_reward->addReward($this->customer->getId(),'普通用户连续签到5天,获得赠送积分10积分',10);
            }
        }
        $this->response->setDataResult(1, '签到成功', $now_sign);
    }


    public function getSignRecords(){
        if (! $this->actionCheck(true, false)) {
            return;
        }
        $this->load->model('account/sign');

        $sign_records=$this->model_account_sign->getSignRecords(date('Y-m-d',strtotime("-1 month")),date('Y-m-d',strtotime('+1 day')));
        $this->response->setDataResult(1, '签到成功', $sign_records);
    }

    public function edit()
    {
        $this->load->model('account/customer');

        if (! $this->actionCheck(true, true)) {
            return;
        }
        if ($this->validate()) {
            $this->model_account_customer->editCustomerByKeyValue($this->request->post['key'], $this->request->post['value']);
            $customer_info = $this->model_account_customer->getCustomer($this->customer->getId());
            $this->load->model('tool/easemob');

            if ($this->request->post['key'] == 'password') {
                $this->model_tool_easemob->setPassword('cook' . $this->customer->getId(), md5($customer_info['password']));
            } else
                if ($this->request->post['key'] == 'nickname') {
                    $this->model_tool_easemob->setNickname('cook' . $this->customer->getId(), $this->request->post['nickname']);
                }
            $this->response->setDataResult(1, "", null);
        } else {
            if (count($this->error) > 0) {
                $error_warning = array_shift($this->error);
            } else {
                $error_warning = '';
            }
            $this->response->setDataResult(0, $error_warning, null);
            return;
        }
    }

    // 获取当前用户今天签到排名
    public  function getTodaySignRank(){
        if (! $this->actionCheck(true, false)) {
            return;
        }
        $this->load->model('account/sign');
        $todayBegin = date('Y-m-d') . " 00:00:00";
        $todayEnd = date('Y-m-d') . " 23:59:59";
        $rank=$this->model_account_sign->getTodaySignRank($todayBegin,$todayEnd);
        $this->response->setDataResult(1, '成功', $rank);
    }
}